package com.tanhua.admin.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.tanhua.admin.service.AnalysisService;
import com.tanhua.admin.utils.ComputeUtil;
import com.tanhua.model.vo.AnalysisSummaryVo;
import com.tanhua.model.vo.AnalysisUsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/dashboard")
public class DashboardController {


    @Autowired
    private AnalysisService analysisService;

    /**
     * 新增、活跃用户、次日留存率
     *  Path：/dashboard/users
     *  Method： GET
     * 请求参数：sd 开始时间
     *          ed 结束时间
     *          type 101 新增 102 活跃用户 103 次日留存率
     * 响应数据：
     */
    @GetMapping("/users")
    public AnalysisUsersVo getUsers(@RequestParam(name = "sd") Long sd
            , @RequestParam("ed") Long ed
            , @RequestParam("type") Integer type) {
       return analysisService.queryAnalysisUsersVo(sd, ed, type);

    }

    /**
     * 概要统计信息
     *  Path：/dashboard/summary
     *  Method： GET
     * 请求参数：map
     * 响应数据：
     */
    @GetMapping("/summary")
    public ResponseEntity summary(){
        //创建AnalysisUsersVo对象
        AnalysisSummaryVo vo=new AnalysisSummaryVo();
        //定义今日日期字符串
        String todaystr =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        //字符串转日期
        DateTime dateTime = DateUtil.parseDate(todaystr);
        //获取昨日日期字符串
        String yesdayStr = DateUtil.yesterday().toString("yyyy-MM-dd");


        //获取累计用户数量(累加所有注册人数)
        Integer registerCount = analysisService.querySumRegister();
        vo.setCumulativeUsers(Long.valueOf(registerCount));

        //获取过去30天活跃用户数量
        Integer activeUserCount = analysisService.queryactiveUserCount(dateTime,-30);
        vo.setActivePassMonth(Long.valueOf(activeUserCount));

        //获取过去7天活跃用户数量
        activeUserCount = analysisService.queryactiveUserCount(dateTime,-7);
        vo.setActivePassWeek(Long.valueOf(activeUserCount));

        //今日新增用户(今天注册的人数)
        Integer NewUserCount =analysisService.queryNewUserCount(todaystr);
        vo.setNewUsersToday(Long.valueOf(NewUserCount));

        //今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        //查找昨日注册用户数量
        Integer yesdayUserCount = analysisService.queryNewUserCount(yesdayStr);
        //获取今日新增用户数量
        Long newUsersToday = vo.getNewUsersToday();
        //今天新增用户的数量与昨日注册用户的数量进行计算
        BigDecimal computeRate1 = ComputeUtil.computeRate(newUsersToday, Long.valueOf(yesdayUserCount));
        vo.setNewUsersTodayRate(computeRate1);

        //今日登录次数
        Integer loginCount = analysisService.queryLoginCount(todaystr);
        vo.setLoginTimesToday(Long.valueOf(loginCount));

        //今日登录次数涨跌率，单位百分数，正数为涨，负数为跌
        //查找昨日登录次数
        Integer yesdayloginCount = analysisService.queryLoginCount(yesdayStr);
        //获取今日登录次数
        Long loginTimesToday = vo.getLoginTimesToday();
        //今天登录次数与昨日注册次数进行计算
        BigDecimal computeRate2 = ComputeUtil.computeRate(loginTimesToday, Long.valueOf(yesdayloginCount));
        vo.setLoginTimesTodayRate(computeRate2);

        //今日活跃用户数量
        Integer ActiveUserCount = analysisService.queryActiveUser(todaystr);
        vo.setActiveUsersToday(Long.valueOf(ActiveUserCount));

        //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        //查找昨日活跃用户数量
        Integer yesdayActiveUser = analysisService.queryActiveUser(yesdayStr);
        //获取昨日活跃用户数量
        Long activeUsersToday = vo.getActiveUsersToday();
        //今天活跃用户的数量与昨日活跃用户的数量进行计算
        BigDecimal computeRate3 = ComputeUtil.computeRate(activeUsersToday, Long.valueOf(yesdayActiveUser));
        vo.setActiveUsersTodayRate(computeRate3);

        return ResponseEntity.ok(vo);
    }
}
